package nautilus.lab.activity;

import nautilus.lab.MainActivity;
import nautilus.lab.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class ScreenSlidePageActivity extends FragmentActivity{
	private static final int NUM_PAGES = 5;
	
	private ViewPager mPager;
	private PagerAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedState){
		super.onCreate(savedState);
		setContentView(R.layout.slide_paper);
		
		mPager = (ViewPager) findViewById(R.id.pager);
		adapter = new ScreenSlidePageAdapter(getSupportFragmentManager()); 
		mPager.setAdapter(adapter);
		
	}
	
	@Override
	public void onBackPressed(){
		if(mPager.getCurrentItem()==0){
			super.onBackPressed();
			return;
		}
		
		mPager.setCurrentItem(mPager.getCurrentItem() - 1);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.slide_page_menu, menu);

        menu.findItem(R.id.action_previous).setEnabled(mPager.getCurrentItem() > 0);

        // Add either a "next" or "finish" button to the action bar, depending on which page
        // is currently selected.
        MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
                (mPager.getCurrentItem() == adapter.getCount() - 1)
                        ? R.string.action_finish: R.string.action_next);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Navigate "up" the demo structure to the launchpad activity.
                // See http://developer.android.com/design/patterns/navigation.html for more.
                NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
                return true;

            case R.id.action_previous:
                // Go to the previous step in the wizard. If there is no previous step,
                // setCurrentItem will do nothing.
                mPager.setCurrentItem(mPager.getCurrentItem() - 1);
                return true;

            case R.id.action_next:
                // Advance to the next step in the wizard. If there is no next step, setCurrentItem
                // will do nothing.
                mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
	
	protected class ScreenSlidePageAdapter extends FragmentStatePagerAdapter{

		public ScreenSlidePageAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int index) {
			return TextPageFragment.create(index);
		}

		@Override
		public int getCount() {
			return NUM_PAGES;
		}
		
	}
}
